package eclipse.weibo.modle;

import java.net.URL;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import weibo4j.Comment;
import weibo4j.DirectMessage;
import weibo4j.Status;
import eclipse.weibo.util.ReadImageUtil;

public class ReadMessageImageThread extends Thread {
    private Table table;
    private Display display;
    private URL[] urls;

    public ReadMessageImageThread(Table table, URL[] urls, Display display) {
        this.table = table;
        this.display = display;
        this.urls = urls;
    }

    public void run() {
        for (URL url : this.urls) {
            ReadImageUtil.read(url);
        }

        this.display.asyncExec(new TThread());
    }

    class TThread extends Thread {
        public void run() {
            TableItem[] tis = ReadMessageImageThread.this.table.getItems();
            for (TableItem ti : tis) {
                Object d = ti.getData();
                URL url = null;
                ImageData id;
                if (d instanceof Status) {
                    Status s = (Status) d;
                    url = s.getUser().getProfileImageURL();
                    id = ReadImageUtil.read(url);
                    ti.setImage(0, new Image(ti.getDisplay(), id));
                    ti.setText(0, "");
                } else if (d instanceof Comment) {
                    Comment c = (Comment) d;
                    url = c.getUser().getProfileImageURL();
                    id = ReadImageUtil.read(url);
                    ti.setImage(0, new Image(ti.getDisplay(), id));
                    ti.setText(0, "");
                } else if (d instanceof DirectMessage) {
                    DirectMessage c = (DirectMessage) d;
                    url = c.getSender().getProfileImageURL();
                    id = ReadImageUtil.read(url);
                    ti.setImage(0, new Image(ti.getDisplay(), id));
                    ti.setText(0, "");
                }
            }
        }
    }
}
